﻿<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="upload.aspx.cs" Inherits="newBlog.CenterInc.view_inc.upload" %>
<%@ Import Namespace="newBlog.CenterInc.Ctrltor" %>
<%@ Import Namespace="newBlog.CenterInc.model" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>
<% 
    
    //Request.Files.Count 客户端传了多少文件过来。此时不论我们使用的是服务端控件还是 HTML 控件，因为到了客户端都是一样的 <input type="file"；另外，即使文件上传框中没有选择文件，都可能会当作上传了无内容的文件（视客户端浏览器）。
    //Request.Files[i].ContentLength 获取上传文件的大小，以字节为单位。
    //Request.Files[i].ContentType 获取客户端发送的文件的 MIME 内容类型。
    //Request.Files[i].FileName
    //Request.Files[i].InputStream
    //Request.Files[i].SaveAs(string filename)
    //上面的 Request.Files[i]，也可以是 Request.Files[name]。
    //string filePath = Server.MapPath("~/updata/portrait/");//上传目录
    //string filename = Request.Files["updata_portrait"].FileName;//文件名
    Response.ContentType = "application/json";
    if (Session["user_id"]==null) {
        Response.Write("{\"error\":\"您未登录本站\"}");
        return;
    }
    
    int mode = Request.QueryString["mode"]==null?0:Convert.ToInt32(Request.QueryString["mode"]);//1文章上传 2头像上传
    int fileCount = Request.Files.Count;
    if (fileCount==0) {
        Response.Write("{\"error\":\"请上传图片\"}");
        return;
    }
    Upload_inc upload = new Upload_inc();
    int i = 0;
    List<upload_Dao> list = new List<upload_Dao>();
    JavaScriptSerializer serializer = new JavaScriptSerializer();
    string json = "";
    int fileSize = 0;
    string mimeNmae="";
    string error="";
    string filePath = "";
    string filename = "";
    switch (mode) {
        case 1://编辑器代码

            for (;i<fileCount;i++) {
                fileSize = Request.Files[i].ContentLength;
                mimeNmae = Request.Files[i].ContentType;
                error = upload.article(fileSize,mimeNmae);
                if (error!=null) {
                    continue;
                }
                filePath = Server.MapPath("~/updata/article/");
                filename = "up_"+DateTime.Now.ToString("yyyyMMddHHmmssffff")+".png";
                Request.Files[i].SaveAs(filePath+filename);
                upload_Dao upload_Dao = new upload_Dao("/updata/article/"+filename,Request.Files[i].FileName,"/updata/article/"+filename);
                list.Add(upload_Dao);
            }
            json = "{\"errno\":0,\"data\":"+serializer.Serialize(list)+"}";
            Response.Write(json);
            return;
        case 2:
            fileSize = Request.Files["updata_portrait"].ContentLength;
            mimeNmae = Request.Files["updata_portrait"].ContentType;
            error = upload.portrait(fileSize,mimeNmae);
            if (error!=null) {
                Response.Write("{\"error\":\""+error+"\"}");
                return;
            }
            User_inc user_Inc = new User_inc();
            filePath = Server.MapPath("~/updata/portrait/");
            filename = Session["user_id"]+".png";
            Request.Files["updata_portrait"].SaveAs(filePath+filename);
            if (user_Inc.UpUserPortrait("/updata/portrait/"+filename,Session["user_id"].ToString())) { 
            Response.Write("{\"error\":\"数据库更新失败\"}");
            return;
            }
            Response.Write("{\"msg\":\"上传成功\"}");
            return;
    }
    %>
